<template>

  <div>
    <!-- 面包屑   -->
    <div style="margin-bottom: 30px">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>建筑购物信息管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <!-- 新增和删除按钮-->
    <div style="margin: 10px 0">
      <el-button type="primary" @click="handleAdd" class="mr-5">新增 <i class="el-icon-circle-plus-outline"></i>
      </el-button>
      <el-popconfirm
          confirm-button-text='确定'
          cancel-button-text='再想想'
          icon="el-icon-info"
          icon-color="red"
          title="确定批量删除吗？"
          @confirm="delBatch">
        <el-button type="danger" slot="reference" class="mr-5">批量删除 <i class="el-icon-remove-outline"></i></el-button>
      </el-popconfirm>
    </div>
    <!-- 表的主体   -->
    <el-table :data="tableData" border stripe
              @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="40">
      </el-table-column>
      <el-table-column prop="buildId" label="编号">
      </el-table-column>
      <el-table-column prop="buildType" label="建筑种类">
      </el-table-column>
      <el-table-column prop="buildLocation" label="建筑位置">
      </el-table-column>
      <el-table-column prop="buildArea" label="建筑总面积">
      </el-table-column>
      <el-table-column prop="floorArea" label="每层面积">
      </el-table-column>
      <el-table-column prop="develop" label="开发商">
      </el-table-column>
      <el-table-column prop="management" label="物业">
      </el-table-column>
      <el-table-column prop="managementExpense" label="物业费">
      </el-table-column>
      <el-table-column prop="management" label="物业">
      </el-table-column>
      <el-table-column prop="parkingNmu" label="停车位">
      </el-table-column>
      <el-table-column prop="rentTime" label="租售时间">
      </el-table-column>
      <el-table-column prop="rentTel" label="租售电话">
      </el-table-column>
      <el-table-column prop="rentExpense" label="租售费用">
      </el-table-column>
      <el-table-column label="操作" width="" align="center" min-width="100">
        <template slot-scope="scope">
          <el-button type="success" @click="handleEdit(scope.row)" class="mr-5">
            查看<i class="el-icon-edit"></i>
          </el-button>
          <el-popconfirm
              confirm-button-text='确定'
              cancel-button-text='再想想'
              icon="el-icon-info"
              icon-color="red"
              title="确定删除吗？"
              @confirm="del(scope.row.buildId)">
            <el-button type="danger" slot="reference">删除 <i class="el-icon-remove-outline"></i></el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!-- 页码导航栏   -->
    <div style="padding: 10px 0">
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :page-sizes="[5, 10, 15, 20]"
          :page-size=pageSize
          layout="total, sizes, prev, pager, next, jumper"
          :total=total>
      </el-pagination>
    </div>
    <!--        建筑购物信息的弹窗-->
    <el-dialog title="机电设备信息" :visible.sync="dialogFormVisible" width="70%" :close-on-click-modal="false">
      <el-form :model="buildInfo" :inline="true" :rules="rules" ref="buildInfo">
        <el-form-item label="建筑种类" label-width="100px" prop="buildType">
          <el-input v-model="buildInfo.buildType" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="建筑位置" label-width="100px" prop="buildLocation">
          <el-input v-model="buildInfo.buildLocation" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="建筑总面积" label-width="100px" prop="buildArea">
          <el-input v-model="buildInfo.buildArea" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="楼层数" label-width="100px" prop="floorNum">
          <el-input v-model="buildInfo.floorNum" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="每层面积" label-width="100px" prop="floorArea">
          <el-input v-model="buildInfo.floorArea" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="开发商" label-width="100px" prop="develop">
          <el-input v-model="buildInfo.develop" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="物业" label-width="100px" prop="management">
          <el-input v-model="buildInfo.management" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="物业费" label-width="100px" prop="managementExpense">
          <el-input v-model="buildInfo.managementExpense" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="停车位" label-width="100px" prop="parkingNmu">
          <el-input v-model="buildInfo.parkingNmu" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="租售时间" label-width="100px" prop="rentTime">
          <el-input v-model="buildInfo.rentTime" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="租售电话" label-width="100px" prop="rentTel">
          <el-input v-model="buildInfo.rentTel" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="租售费用" label-width="100px" prop="rentExpense">
          <el-input v-model="buildInfo.rentExpense" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible=false">取 消</el-button>
        <el-button type="primary" @click="save('buildInfo')">{{ btnText }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>



<script>
import request from "@/utils/request";
import {country} from "@/js/country";

export default {
  name: "BuildInfo",
  data() {
    return {
      tableData: [],
      currentPage: 1,
      name: "",
      username: "",
      gender: "",
      pageSize: 5,
      total: 0,
      dialogFormVisible: false,
      btnText: "",
      buildInfo: {},
      multipleSelection: [],
      rules: {
        // name: [
        //   {required: true, message: '请输入姓名', trigger: 'blur'},
        // ],
        // gender: [
        //   {required: true, message: '请选择性别', trigger: 'blur'},
        // ],
        // phone: [
        //   {required: true, message: '请输入电话号码', trigger: 'blur'},
        //   {min: 11, max: 11, message: '请输入长度为11个数字的电话号码', trigger: 'blur'}
        // ],
        // username: [
        //   {required: true, message: '请输入用户名', trigger: 'blur'},
        //   {min: 3, message: '长度在 3 个字符以上', trigger: 'blur'}
        // ],
        // password: [
        //   {required: true, message: '请输入密码', trigger: 'blur'},
        //   {min: 6, message: '长度在 6 个字符以上', trigger: 'blur'}
        // ]
      },
    }
  },
  created() {
    this.load();
    request.get("/build/info/getAll").then(res => {
      this.buildInfo = res.data
    })
  },
  methods: {
    // 分页查询接口
    load() {
      request.get("/build/info/getAll",
          {params: {pageNum: this.currentPage, pageSize: this.pageSize}}
      ).then(res => {
        this.tableData = res.data.records
        this.total = res.data.total
        this.currentPage = res.data.current
        this.pageSize = res.data.size
      })
    },
    handleSizeChange(pageSize) {
      this.pageSize = pageSize
      this.load()
    },
    handleCurrentChange(pageNum) {
      this.currentPage = pageNum
      this.load()
    },
    handleAdd() {
      this.btnText = "添加"
      this.buildInfo = {}
      this.dialogFormVisible = true
    },
    handleEdit(row) {
      this.btnText = "修改"
      this.dialogFormVisible = true
      this.buildInfo = Object.assign({}, row)
    },
    // 增加修改数据接口
    save(buildInfo) {
      this.$refs[buildInfo].validate((valid) => {
        if (valid) {
          this.dialogFormVisible = false
          if (this.btnText === '添加') {
            request.post("/build/info/add", this.buildInfo).then(res => {
              if (res) {
                this.$message.success(this.btnText + "成功")
                this.load()
              } else {
                this.$message.error(this.btnText + "失败")
              }
            })
          } else {
            // console.log(this.buildInfo)

            // console.log(this.buildInfo)
            request.put("/build/info/update", this.buildInfo).then(res => {
              if (res) {
                this.$message.success(this.btnText + "成功")
                this.load()
              } else {
                this.$message.error(this.btnText + "失败")
              }
            })
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    // 单个删除
    del(id) {
      request.delete("/build/info/delmore/" + id).then(res => {
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.load()
        } else {
          this.$message.error("删除失败")
        }
      })
      console.log(id)
    },
    handleSelectionChange(val) {
      // console.log(val)
      this.multipleSelection = val
    },
    // 批量删除接口
    delBatch() {
      //map函数：数组的每个元素都执行一个给定的函数，返回值仍为一个数组
      let ids = this.multipleSelection.map(v => v.buildId)
      console.log(ids)
      if (ids.length !== 0) {
        request.delete("/build/info/delmore/" + ids.toString(),).then(res => {
          if (res) {
            this.$message.success("批量删除成功")
            this.load()
          } else {
            this.$message.error("批量删除失败")
          }
        })
      } else {
        this.$message.warning("请选择要删除的数据")
      }
    },
    reset() {
      this.username = ""
      this.name = ""
      this.gender = ""
      this.load()
    },
  }
}
</script>

<style scoped>

</style>